//
//  RecentAnnounce.m
//  DaoZiJinFu
//
//  Created by woker on 16/7/12.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "RecentAnnounce.h"
#import "AnnounceDetailVC.h"

@interface RecentAnnounce () <UITableViewDelegate,UITableViewDataSource> {
    NSMutableArray *listArray;
    NSInteger currentPage;
}

@property (strong, nonatomic) UITableView *announceTable;

@end

@implementation RecentAnnounce

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"公告"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    
    listArray = [NSMutableArray array];
    
    _announceTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _announceTable.delegate = self;
    _announceTable.dataSource = self;
    [self.view addSubview:_announceTable];
    
    _announceTable.mj_header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTableView)];
    _announceTable.mj_footer = [MJDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestForNewData)];
    [_announceTable.mj_header beginRefreshing];
}

- (void)refreshTableView {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"User/getBulletinList";
    request.params = @{@"p":@1,
                       @"pagesize":@10};
    request.httpMethod = @"GET";
    [DZNetAPI request:request success:^(id response) {
        NSLog(@"%@",response);
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            [self dealWithResult:response];
        }else if (model.rt == 0) {
           [CommTools getRnsAndSid:^(id result) {
              [DZNetAPI request:request success:^(id response) {
                 DZResponse *models = [DZResponse mj_objectWithKeyValues:response];
                  if (models.rt == 1) {
                      [self dealWithResult:response];
                  }
              } failure:^(NSError *error) {
              }];
           }];
        }
      [_announceTable.mj_header endRefreshing];
    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
      [_announceTable.mj_header endRefreshing];
    }];
}

- (void)dealWithResult:(id)result {
    if (result == nil) {
        if (userValue(HOSTTEL)) {
            LoginViewController *login = [[LoginViewController alloc] init];
            login.telNum = userValue(HOSTTEL);
            [self.navigationController pushViewController:login animated:YES];
        } else {
            RegisterViewController *join = [[RegisterViewController alloc] init];
            [self.navigationController pushViewController:join animated:YES];
        }
    } else {
        [theUser setObject:@0 forKey:UNREAD_BULLETIN_NUMBER];
        NSArray *arr = result[@"data"][@"list"];
        if (arr != nil) {
            listArray = [NSMutableArray arrayWithArray:arr];
        }
        currentPage = [result[@"data"][@"p"] integerValue];
        [_announceTable reloadData];
    }
}

- (void)requestForNewData {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"User/getBulletinList";
    request.params = @{@"p":[NSString stringWithFormat:@"%ld",currentPage+1],
                       @"pagesize":@10};
    request.httpMethod = @"GET";
    [DZNetAPI request:request success:^(id response) {
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        NSLog(@"%@ %ld",model.msg,model.rt);
        if (model.rt == 1) {
            NSArray *arr = model.data[@"list"];
            if (arr.count) {
                [listArray addObjectsFromArray:arr];
                currentPage = [model.data[@"p"] intValue];
            }
            [_announceTable reloadData];
            [_announceTable.mj_footer endRefreshing];
        }
    } failure:^(NSError *error) {
        [_announceTable.mj_footer endRefreshing];
        JSSHOW(LOAD_FAIL_TIP);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ANNOUNCE_CELL];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ANNOUNCE_CELL];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
//    NSLog(@"data: %@",listArray[indexPath.row]);
    cell.textLabel.text = listArray[indexPath.row][@"title"];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AnnounceDetailVC *announceDetail = [[AnnounceDetailVC alloc]init];
//    announceDetail.title = listArray[indexPath.row][@"title"];
    announceDetail.idStr = listArray[indexPath.row][@"id"];
    [self.navigationController pushViewController:announceDetail animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

@end
